package com.fulin.leetcode;

/**
 * 删除已排序链表中的重复节点
 *
 * @Author fulin
 * @Date 2022/9/19 17:24
 **/
public class Num83 {

    /**
     * 遍历的解法
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head;
        while(cur != null && cur.next != null){
            if(cur.val == cur.next.val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }

        return head;
    }

    /**
     * 递归的解法
     * @param head
     * @return
     */
    public ListNode deleteDuplicates2(ListNode head) {
        if(head == null) return null;

        ListNode sub = deleteDuplicates2(head.next);

        if(sub != null && sub.val == head.val){
            head.next = sub.next;
        }

        return head;
    }
}
